/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TUFormSms.java
 *
 * Created on Nov 9, 2012, 2:44:52 PM
 */
package Form;

import Boundary.OrangTua;
import Boundary.Presensi;
import Boundary.TU;
import Boundary.siswa;
import Control.TUkontrol;
import Table.Smstabelmodel;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Fave
 */
public class TUFormSms extends javax.swing.JFrame {

    /** Creates new form TUFormSms */
    public TUFormSms() {
        initComponents();
        DateChooser.setVisible(false);
        refresh();
        setTitle("Sistem Presensi Akademik Berbasis SMS Gateway");

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        KeluarButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        Tabelsms = new javax.swing.JTable();
        DateChooser = new com.toedter.calendar.JDateChooser();
        KirimSMSButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/banner1.jpg"))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Pengiriman SMS"));

        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        Tabelsms.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "NIS", "Nama", "Status", "Nama Orang Tua", "Nomor HP"
            }
        ));
        jScrollPane2.setViewportView(Tabelsms);

        KirimSMSButton.setText("Kirim SMS");
        KirimSMSButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KirimSMSButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(KirimSMSButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(KeluarButton)
                        .addGap(26, 26, 26))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 742, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(DateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(KirimSMSButton)
                    .addComponent(KeluarButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        // TODO add your handling code here:
        TUForm tf = new TUForm();
        tf.setVisible(true);
        dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void KirimSMSButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KirimSMSButtonActionPerformed
        // TODO add your handling code here:
        int a = 0;
        siswa s = new siswa();
        Presensi p = new Presensi();
        OrangTua o = new OrangTua();
        TU t = new TU();
        Date tgl = this.DateChooser.getDate();
        SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MM-yyyy");
        String tanggal = tglsimple.format(tgl);
        while (a < Tabelsms.getRowCount()) {
            String nama = Tabelsms.getValueAt(a, 1).toString();
            s.setNama(nama);
            t.setNama(s);
            String status = Tabelsms.getValueAt(a, 2).toString();
            if (status.equals("Masuk")) {
                p.setKodekehadiran(1);
            } else if (status.equals("Ijin")) {
                p.setKodekehadiran(2);
            } else if (status.equals("Sakit")) {
                p.setKodekehadiran(3);
            } else {
                p.setKodekehadiran(4);
            }
            t.setStatus(p);
            String no = Tabelsms.getValueAt(a, 4).toString();
            o.setNoTelp(no);
            t.setNohp(o);
            p.setTanggal(tanggal);
            t.setTaggal(p);
            System.out.println(nama);
            System.out.println(status);
            System.out.println(no);
            a = a + 1;

            try {

                TUkontrol.sqlyog().kirimsms(t);


            } catch (SQLException ex) {

                JOptionPane.showMessageDialog(this, "SMS gagal Dikirim");
                System.out.print(ex.getMessage());

            }

        }
        JOptionPane.showMessageDialog(this, "SMS Berhasil Dikirim");
    }//GEN-LAST:event_KirimSMSButtonActionPerformed

    public void refresh() {
        try {

            GregorianCalendar gc = new GregorianCalendar();
            DateChooser.setDate(gc.getTime());
            DateChooser.setEnabled(false);
            Date tgl = this.DateChooser.getDate();
            SimpleDateFormat tglsimple = new SimpleDateFormat("dd-MM-yyyy");
            String tanggal = tglsimple.format(tgl);
            System.out.println(tanggal);
            List<TU> tu = TUkontrol.getKoneksi().getSms(tanggal);

            Smstabelmodel model = new Smstabelmodel(tu);
            Tabelsms.setModel(model);
            //  System.out.println("xxx");
        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TUFormSms().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser DateChooser;
    private javax.swing.JButton KeluarButton;
    private javax.swing.JButton KirimSMSButton;
    private javax.swing.JTable Tabelsms;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
